package cn.com.yang.nio;

import java.nio.ByteBuffer;

/**
 * 缓冲区切片
 * @Author 杨亿金
 * @Date 2021/12/18 21:59
 * @CopyRight
 * @Version
 */
public class BufferSlice {
    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        //缓冲区中的数据0~9
        for (int i = 0; i < byteBuffer.capacity(); i++) {
            byteBuffer.put((byte) i);
        }
        //创建子缓冲区
        byteBuffer.position(3);
        byteBuffer.limit(7);
        ByteBuffer slice = byteBuffer.slice();

        //改变子缓冲区的内容
        for (int i = 0; i <slice.capacity() ; i++) {
            byte b = slice.get(i);
            b *=10;
            slice.put(i,b);
        }
        byteBuffer.position(0);
        byteBuffer.limit(byteBuffer.capacity());
        while (byteBuffer.remaining()>0){
            System.out.println(byteBuffer.get());
        }
    }
}
